//
//  XLReChargeViewController.m
//  XiangLove
//
//  Created by 宋振 on 2018/8/3.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLReChargeViewController.h"

#import "XLEmptyFootView.h"

#import "XLReChagerModel.h"

#import "XLReChagerTableViewCell.h"

#import <MJRefresh.h>

@interface XLReChargeViewController ()<UITableViewDelegate,UITableViewDataSource>

//绘制容器
@property (nonatomic, strong) UITableView *  tableView;

//数据
@property (nonatomic, strong) NSMutableArray *  dataSources;

@property (nonatomic, strong) XLEmptyFootView *  emptyView;

@end

@implementation XLReChargeViewController
{
    //辅助
    NSInteger page;
    
    //是否存在数据
    BOOL haveSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"充值记录";
    [self showBackButton:NULL];
    
    
    /**
     绘制界面
     */
    [self setCustomeView];
    
    /**
     初始化辅助变量
     */
    [self initTools];
    /**
     请求数据
     */
    [self requestSource];
    
}

#pragma mark    - - - 绘制界面 - - -

/**
 绘制界面
 */
- (void)setCustomeView
{
    CGFloat naviHeight = IS_IPhoneX ? 88.0f : 64.0f;
    
    CGRect rect = CGRectMake(0, 0, self.view.bounds.size.width, ScreenHeight-naviHeight);
    
    self.tableView = [[UITableView alloc] initWithFrame:rect];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.backgroundColor = RGB(243, 243, 243);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    
    
    [self.view addSubview:self.tableView];
    
    //设置下拉刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        
        [self requestSource];
        
    }];
    
    //注册
    [self.tableView registerClass:[XLReChagerTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XLReChagerTableViewCell class])];
}

#pragma mark    - - - 辅助变量 - - -
- (void)initTools
{
    page = 1;
    haveSource = YES;
}

#pragma mark    - - - 请求数据信息 - - -

/**
 请求消费数据记录信息
 */
- (void)requestSource
{
    //组织参数
    
    NSDictionary *dic = @{
                          @"userId":[[XLAccountManager shareInstance] model].Id,
                          @"pageIndex":@(page),
                          @"pageSize":@"10"
                          };
    
    [[[XLHTTPSessionManager sharedManager] XLV2GET:@"/api/v2/Balance/GetPayRecord" parameters:dic resultClass:[XLReChagerModel class]] subscribeNext:^(id x) {
        
        if (x == nil || [x isKindOfClass:[NSNull class]]) {
            
            return ;
        }
        
        if (page == 1) {
            [self.dataSources removeAllObjects];
        }
        
        [self.dataSources addObjectsFromArray:x];
        
        if (self.dataSources.count > 0) {
            page++;
        }
        
        //是否还有数据
        if (self.dataSources.count < page*10 || self.dataSources.count == 0) {
            self.tableView.mj_footer.hidden = YES;
        }else{
            self.tableView.mj_footer.hidden = NO;
        }
        
        if (self.dataSources.count > 0) {
            haveSource = YES;
        }else{
            haveSource = NO;
        }
        
        [self.tableView reloadData];
        
        
        [self.tableView.mj_footer endRefreshing];
        
    } error:^(NSError *error) {
        
        if (self.dataSources.count > 0 ) {
            [self.dataSources removeAllObjects];
            haveSource = NO;
        }
        [self.tableView reloadData];
        
        [self.tableView.mj_footer endRefreshing];
        
    }];
}


#pragma mark    -   -   - 系统代理 -    -   -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSources.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XLReChagerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XLReChagerTableViewCell class]) forIndexPath:indexPath];
    
    XLReChagerModel *model = self.dataSources[indexPath.row];
    
    cell.model = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 86;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (haveSource == NO) {
        XLEmptyFootView *foot = [[XLEmptyFootView alloc] init];
        foot.refreshReach = ^{
            
            [self requestSource];
            
        };
        
        return foot;
    }
    
    
                             
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (haveSource == YES) {
        return 0.1;
        
    }else{
        return self.view.bounds.size.height;
    }
}




#pragma mark    - - -lazy- - -

- (NSMutableArray *)dataSources
{
    if (!_dataSources) {
        self.dataSources = [NSMutableArray arrayWithCapacity:1];
    }
    
    return _dataSources;
}

@end
